package com.ixiaowo.springcouldconsumermovie.controller;


import com.ixiaowo.springcouldconsumermovie.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class MoiveController {
    /**注入模板*/
    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private LoadBalancerClient loadBalancerClient;
    /**
     * 消费微服务
     * @param id
     * @return
     */
    @GetMapping("/movie/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallback") // 添加 hystrix注解
    public User findById(@PathVariable Long id){
      return  this.restTemplate.getForObject("http://microservice-provider-user/simple/"+id, User.class);
    }

    /**
     * 添加 hystrix默认的方法!
     * @param id
     * @return
     */
    public User findByIdFallback(Long id){
        User user = new User();
        user.setId(0L);
        return user;
    }

    @GetMapping("/movies")
    public User getString(){
        return  this.restTemplate.getForObject("http://microservice-provider-user/simple/"+1, User.class);
    }
}
